package org.darcy.service.base.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.darcy.entity.base.BaseDicType;
import org.darcy.entity.base.BaseDictionary;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.Query;
import org.darcy.service.base.BaseDicTypeService;
import org.darcy.service.base.BaseDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Service
public class BaseDictionaryServiceImpl extends GenericServiceImpl<BaseDictionary> implements BaseDictionaryService {

	@Autowired
	private BaseDicTypeService BaseDicTypeService;

	@Override
	public PageData<BaseDictionary> page(PageParam<BaseDictionary> param) {
		BaseDictionary dictionary = param.getEntity();
		Query query = param.getQuery();
		LambdaQueryWrapper<BaseDictionary> dictionaryLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(dictionary.getCode())) {
			dictionaryLambdaQueryWrapper.like(BaseDictionary::getCode, dictionary.getCode()).or()
					.like(BaseDictionary::getValue, dictionary.getCode());
		}
		dictionaryLambdaQueryWrapper.eq(BaseDictionary::getPid, dictionary.getPid());
		IPage<BaseDictionary> queryPage = new Page<>(query.getPageNum(), query.getPageSize());
		IPage<BaseDictionary> page = this.getPage(queryPage, dictionaryLambdaQueryWrapper);
		PageData<BaseDictionary> pageData = new PageData<BaseDictionary>(page, param);
		return pageData;
	}

	@Override
	public List<BaseDictionary> getList(String typeName) {
		// "委托业务", "检测类别"
		LambdaQueryWrapper<BaseDicType> typeQuery = new LambdaQueryWrapper<>();
		typeQuery.eq(BaseDicType::getName, typeName);
		BaseDicType type = BaseDicTypeService.getOne(typeQuery);

		LambdaQueryWrapper<BaseDictionary> q = new LambdaQueryWrapper<>();
		q.eq(BaseDictionary::getPid, type.getId());
		return this.getList(q);
	}

}
